/* eslint-disable @typescript-eslint/no-explicit-any */
import {
  usePostCommentApi,
  usePostGetAllPostsApi,
  usePostGetCommentByPostIdApi,
} from '@/api/post'
import { defineStore } from 'pinia'
import { onMounted, ref, computed } from 'vue'
import { useUserStore } from './user'

export const usePostStore = defineStore('post', () => {
  const userStore = useUserStore()
  const posts = ref<any>([])
  onMounted(() => {
    setInterval(() => {
      // TODO: 从后端获取所有帖子
      updatePost()
    }, 1000)
  })

  const updatePost = () => {
    // TODO: 从后端获取所有帖子
    usePostGetAllPostsApi().then((res) => {
      posts.value = res.data.data
      // 对帖子按时间排序
      posts.value.sort(
        (
          a: {
            post: { createTime: string | number | Date }
          },
          b: {
            post: { createTime: string | number | Date }
          },
        ) => {
          return (
            new Date(b.post.createTime).getTime() -
            new Date(a.post.createTime).getTime()
          )
        },
      )
    })
  }

  // 将 getPostOfSelf 改为 computed 计算属性
  const getPostOfSelf = computed(() => {
    return posts.value.filter(
      (post: { post: { userId: number } }) =>
        post.post.userId == userStore.user.id,
    )
  })

  const getNoticePosts = computed(() => {
    return posts.value.filter(
      (post: { post: { topic: any } }) =>
        post.post.topic == '公告',
    )
  })

  const getSportPosts = computed(() => {
    return posts.value.filter(
      (post: { post: { topic: any } }) =>
        post.post.topic == '体育',
    )
  })

  const getEducationPosts = computed(() => {
    return posts.value.filter(
      (post: { post: { topic: any } }) =>
        post.post.topic == '学习',
    )
  })
  const getLifePosts = computed(() => {
    return posts.value.filter(
      (post: { post: { topic: any } }) =>
        post.post.topic == '生活',
    )
  })

  // 将 getPostById 改为普通函数
  const getPostById = (id: number) => {
    return posts.value.find(
      // 假设 post 对象直接有 id 属性，根据实际情况调整
      (post: { post: { id: number } }) => {
        console.log(post)
        return post.post.id == id
      },
    )
  }

  // 根据标题模糊查询
  const getPostsByTitle = (keyword: string) => {
    return posts.value.filter(
      (post: { post: { title: string } }) =>
        post.post.title
          .toLowerCase()
          .includes(keyword.toLowerCase()),
    )
  }

  // 根据内容和标题模糊查询
  const getPostsByTitleAndContent = (keyword: string) => {
    return posts.value.filter(
      (post: {
        post: { title: string; content: string }
      }) =>
        post.post.title
          .toLowerCase()
          .includes(keyword.toLowerCase()) ||
        post.post.content
          .toLowerCase()
          .includes(keyword.toLowerCase()),
    )
  }

  // 根据用户名模糊查询文章
  const getPostsByUsername = (keyword: string) => {
    return posts.value.filter(
      // 修改属性名
      (post: { userName: string }) =>
        post.userName
          .toLowerCase()
          .includes(keyword.toLowerCase()),
    )
  }

  return {
    posts,
    updatePost,
    getPostOfSelf,
    getPostById,
    getNoticePosts,
    getSportPosts,
    getEducationPosts,
    getLifePosts,
    getPostsByTitle,
    getPostsByTitleAndContent,
    getPostsByUsername,
  }
})
